<?php

use yii\db\Migration;

/**
 * Class m230605_094513_rbac_furniture
 */
class m230605_094513_rbac_furniture extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $auth = Yii::$app->authManager;

        $permissionCreate = $auth->createPermission('createFurniture');
        $auth->add($permissionCreate);

        $permissionUpdate = $auth->createPermission('updateFurniture');
        $auth->add($permissionUpdate);

        $permissionDelete = $auth->createPermission('deleteFurniture');
        $auth->add($permissionDelete);

        $permissionBrowse = $auth->createPermission('browseFurniture');
        $auth->add($permissionBrowse);

        $userRole = $auth->createRole('furnitureUser');
        $auth->add($userRole);

        $managerRole = $auth->createRole('furnitureManager');
        $auth->add($managerRole);

        $auth->addChild($userRole, $permissionBrowse);

        $auth->addChild($managerRole, $permissionCreate);
        $auth->addChild($managerRole, $permissionUpdate);
        $auth->addChild($managerRole, $permissionDelete);
        $auth->addChild($managerRole, $permissionBrowse);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $auth = Yii::$app->authManager;

        $auth->remove($auth->getPermission('createFurniture'));
        $auth->remove($auth->getPermission('updateFurniture'));
        $auth->remove($auth->getPermission('deleteFurniture'));
        $auth->remove($auth->getPermission('browseFurniture'));
        $auth->remove($auth->getRole('furnitureUser'));
        $auth->remove($auth->getRole('furnitureManager'));
    }
}
